<?php
/**
 * @author : Plun
 * @name : Bootstrap
 * @version : 201209
 * @copyright : Dream-Weavers
 */
require_once 'Zend'. DS .'Loader'. DS .'Autoloader.php';

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Front Controller instance
     */
    protected static $_front;

    /**
     * View instance
     * @var Zend_View
     */
    protected static $_view;

    /**
     * Registration of autoloader
     */
    protected function _initAutoloader()
    {
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Plun_');
    }

    /**
     * Initialize session
     */
    protected function _initSession()
    {
        Plun_Session::init();
    }

    /**
     * Defined VARIABLES
     */
    protected function _initDefine()
    {
        $config = Plun_Config::get('application');
        define('SITE_URL', $config->site_url);
        define('STATIC_URL', $config->static_url);
        define('IS_AJAX', !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
        define('LOADING', '<div style="text-align:center; padding: 25px;"><img align="center" src="' . STATIC_URL . '/v1/images/graphics/editor_loading.gif"/></div>');
        define('EMPTY_TABLE', '<tr><td colspan="100">Không có dữ liệu</tr>');
    }

    /**
     * Zend_Front_Controller created on each request
     */
    protected function _initFrontController()
    {
        // Init frontController
        self::$_front = Zend_Controller_Front::getInstance();

        /* Setup front controller */
        self::$_front->addModuleDirectory(APPLICATION_PATH . DS . 'modules')
            ->setDefaultModule('default')
            ->setDefaultControllerName('index')
            ->setDefaultAction('index')
            //->setParam('useDefaultControllerAlways', true)
            ->setParam('env', APP_ENV)
            ->throwExceptions(APP_ENV != 'production');
        //Set new router
        self::initRouters();
        //return self::$_front;
    }

    /**
     * Init Zend_View
     * @return Zend_View
     */
    protected function _initView()
    {
        self::$_view = Zend_Layout::startMvc()->getView();
        self::$_view->addScriptPath(APPLICATION_PATH . DS . 'layouts');
        self::$_view->addHelperPath(APPLICATION_PATH . DS . 'layouts'. DS .'helpers');
        return self::$_view;
    }

    /**
     * Routers setup
     */
    protected static function initRouters()
    {
        //Default module
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'default');
        $default_router = new Zend_Controller_Router_Route(':controller/:action/*',array('controller' => 'index', 'action' => 'index', 'module' => 'default'));

        //Admin module
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/backend/controllers', 'backend');
        $admin_router = new Zend_Controller_Router_Route('backend/:controller/:action/*',array('controller' => 'index', 'action' => 'index', 'module' => 'backend'));

        //info/company
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'company_info');
        $info_company = new Zend_Controller_Router_Route_Regex('company-info.html',array('module' => 'default', 'controller' => 'info', 'action' => 'company'),array(),'company-info.html');

        //info/advertising
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'advertising_info');
        $info_advertising = new Zend_Controller_Router_Route_Regex('advertising-info.html',array('module' => 'default', 'controller' => 'info', 'action' => 'advertising'),array(),'advertising-info.html');

        //info/community
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'community_guidelines');
        $info_community = new Zend_Controller_Router_Route_Regex('community-guidelines.html',array('module' => 'default', 'controller' => 'info', 'action' => 'community'),array(),'community-guidelines.html');

        //info/privacy
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'privacy_policy');
        $info_privacy = new Zend_Controller_Router_Route_Regex('privacy-policy.html',array('module' => 'default', 'controller' => 'info', 'action' => 'privacy'),array(),'privacy-policy.html');
        
        //info/contact
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'contact_us');
        $info_contact = new Zend_Controller_Router_Route_Regex('contact-us.html',array('module' => 'default', 'controller' => 'info', 'action' => 'contact'),array(),'contact-us.html');

        //info/help
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'help');
        $info_help = new Zend_Controller_Router_Route_Regex('help.html',array('module' => 'default', 'controller' => 'info', 'action' => 'help'),array(),'help.html');

        //illbethere/index
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'ill_be_there');
        $illbethere_index = new Zend_Controller_Router_Route_Regex('ill-be-there.html',array('module' => 'default', 'controller' => 'illbethere', 'action' => 'index'),array(),'ill-be-there.html');
        
        //hestalked/index
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'he_talked_you');
        $hetalked_index = new Zend_Controller_Router_Route_Regex('he-talked-you.html',array('module' => 'default', 'controller' => 'hetalked', 'action' => 'index'),array(),'he-talked-you.html');

        //setting/candy
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'candy_jar');
        $setting_candy = new Zend_Controller_Router_Route_Regex('candy-jar.html',array('module' => 'default', 'controller' => 'setting', 'action' => 'candy'),array(),'candy-jar.html');

        //setting/privacy
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'setting_privacy');
        $setting_privacy = new Zend_Controller_Router_Route_Regex('privacy.html',array('module' => 'default', 'controller' => 'setting', 'action' => 'privacy'),array(),'privacy.html');

        //setting/profile
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'setting_profile');
        $setting_profile = new Zend_Controller_Router_Route_Regex('profile.html',array('module' => 'default', 'controller' => 'setting', 'action' => 'profile'),array(),'profile.html');

        //isu/index
        self::$_front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'i_saw_u');
        $isu_index = new Zend_Controller_Router_Route_Regex('i-saw-u.html',array('module' => 'default', 'controller' => 'isu', 'action' => 'index'),array(),'i-saw-u.html');

        //Add router
        $routers = self::$_front->getRouter();
		$routers->removeDefaultRoutes();
        $routers->addRoute('default', $default_router);
        $routers->addRoute('backend', $admin_router);
        $routers->addRoute('company_info', $info_company);
        $routers->addRoute('advertising_info', $info_advertising);
        $routers->addRoute('community_guidelines', $info_community);
        $routers->addRoute('privacy_policy', $info_privacy);
        $routers->addRoute('contact_us', $info_contact);
        $routers->addRoute('help', $info_help);
        $routers->addRoute('ill_be_there', $illbethere_index);
        $routers->addRoute('he_talked_you', $hetalked_index);
        $routers->addRoute('candy_jar', $setting_candy);
        $routers->addRoute('setting_privacy', $setting_privacy);
        $routers->addRoute('setting_profile', $setting_profile);
        $routers->addRoute('i_saw_u', $isu_index);
        
        //Set new router
        self::$_front->setRouter($routers);

        //Cleanup
        unset($routers, $default_router, $admin_router);
    }

    /**
     * Plugin Front Init
     */
    protected function initFrontPlugin()
    {
        //Add Plugin        
        self::$_front->registerPlugin(new Plun_Plugin_Auth());
        //Add Env Plugin
        self::$_front->registerPlugin(new Plun_Plugin_Env());
    }

    /**
     * Init zend navigation
     * @return type
     */
    protected function _initNavigation()
    {
//        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml');
//        $navigation = new Zend_Navigation($config);
//
//        self::$_view->navigation($navigation);
    }

    /**
     * Run the application
     *
     * Checks to see that we have a default controller directory. If not, an
     * exception is thrown.
     *
     * If so, it registers the bootstrap with the 'bootstrap' parameter of
     * the front controller, and dispatches the front controller.
     *
     * @return void
     * @throws Zend_Exception
     */
    public function run()
    {
        // Check default module
        if (null === self::$_front->getControllerDirectory(self::$_front->getDefaultModule())) {
            throw new Zend_Exception('No default controller directory registered with front controller');
        }

        self::initFrontPlugin();
		
        // Dispatch controller
        self::$_front->setParam('bootstrap', $this)->dispatch();
    }

}